import { defineStore } from 'pinia'
import { ref, computed } from 'vue'

export const useUserStore = defineStore('user', () => {
  // 状态
  const user = ref({
    id: 0,
    name: '',
    email: '',
    isLoggedIn: false
  })
  
  const token = ref('')
  
  // getter
  const fullName = computed(() => {
    return user.value.name ? `${user.value.name} (ID: ${user.value.id})` : '未登录'
  })
  
  // actions
  function login(username: string, password: string) {
    // 模拟API调用
    setTimeout(() => {
      user.value = {
        id: 1,
        name: username,
        email: `${username}@example.com`,
        isLoggedIn: true
      }
      token.value = 'mock-jwt-token'
    }, 1000)
  }
  
  function logout() {
    user.value = {
      id: 0,
      name: '',
      email: '',
      isLoggedIn: false
    }
    token.value = ''
  }
  
  return {
    user,
    token,
    fullName,
    login,
    logout
  }
}) 